package com.bangf.api;


import com.bangf.config.Code;
import com.bangf.domain.*;
import com.bangf.service.SelectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/select")
@Api(value = "选择查询的接口")
public class SelectApi {

    @Autowired
    private SelectService selectS;

    /**
     * @deprecation 查询所有家庭成员
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-07 10:41
     */
    @ResponseBody
    @RequestMapping("/findAllResident")
    @ApiOperation(value = "查询所有家庭成员",notes = "test: 这是个notes")
    public Map<String,Object> findAllResident(){
        Map<String,Object> map = new HashMap<>();

        map.put("code", Code.SUCCESS);
        map.put("msg", "这是一个测试接口");
        return map;
    }


    /**
     * @deprecation 查询所有的乡镇
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-07 14:11
     */
    @ResponseBody
    @RequestMapping("/findAllTown")
    public Map<String,Object> findAllTown(){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        //获取所有的乡镇
        List<FpTown> townList = selectS.findAllTown();
        map.put("code",Code.SUCCESS);
        map.put("msg","操作成功");

        //data
        data.put("townList",townList);
        map.put("data",data);

        return map;
    }


    /**
     * @deprecation 根据乡镇id，查询该乡镇的所有村庄
     * @param townId Integer townId
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-07 14:33
     */
    @ResponseBody
    @RequestMapping("/findAllVillage")
    public Map<String,Object> findAllVillage(@Param("townId") Integer townId){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        if(townId == null){
            map.put("code",Code.PARAM_LESS);
            map.put("msg","缺少请求参数");
        }else if(selectS.checkHasTown(townId)){//检测乡镇是否存在
            //获取乡镇
            List<FpVillage> villageList = selectS.findAllVillageByTownId(townId);
            data.put("villageList",villageList);
            map.put("code",Code.SUCCESS);
            map.put("msg","操作成功");
            map.put("data",data);
        }else{
            map.put("code",Code.TOWN_NOT_FIND);
            map.put("msg","该乡镇不存在");
        }

        return map;
    }

    /**
     * @deprecation 根据村庄id获取人口统计
     * @param villageId Integer 村庄id
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-07 14:57
     */
    @ResponseBody
    @RequestMapping("/findVillageUserCount")
    public Map<String,Object> findVillageUserCount(@Param("villageId") Integer villageId){

        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        if(villageId == null){
            map.put("code",Code.PARAM_LESS);
            map.put("msg","缺少请求参数");
        }else if(selectS.checkHasVillage(villageId)){//检测村庄是否存在
            //获取村庄人口统计
            FpVillageSum villageSum = selectS.findVillageSumByVillageId(villageId);
            data.put("villageSum",villageSum);

            map.put("code",Code.SUCCESS);
            map.put("msg","操作成功");
            map.put("data",data);
        }else{
            map.put("code",Code.VILLAGE_NOT_FIND);
            map.put("msg","该村庄不存在");
        }
        return map;
    }

    /**
     * @deprecation 根据乡镇id获取人口统计
     * @param townId Integer 乡镇id
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-07 14:57
     */
    @ResponseBody
    @RequestMapping("/findTownUserCount")
    public Map<String,Object> findTownUserCount(@Param("townId") Integer townId){

        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        if(townId == null){
            map.put("code",Code.PARAM_LESS);
            map.put("msg","缺少请求参数");
        }else if(selectS.checkHasTown(townId)){//检测乡镇是否存在
            //获得乡镇人口统计
            FpTownSum townSum = selectS.findTownSumByTownId(townId);
            data.put("townSum",townSum);

            map.put("code",Code.SUCCESS);
            map.put("msg","操作成功");
            map.put("data",data);
        }else{
            map.put("code",Code.VILLAGE_NOT_FIND);
            map.put("msg","该村庄不存在");
        }
        return map;
    }

    /**
     * @deprecation 获取乡镇的集体经济排行
     * @param rank Integer 排行数
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-08 8:58
     */
    @ResponseBody
    @RequestMapping("/selectTownRank")
    public Map<String,Object> selectTownRank(@Param("rank") Integer rank){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        if(rank == null){
            map.put("code",Code.PARAM_LESS);
            map.put("msg","缺少请求参数");
        }else if(selectS.checkHasTownRank(rank)){//检测排名长度是否超出

            List<FpTownSum> townSumList = selectS.findTownSumOrderByAllIncome(rank);
            data.put("townSumList",townSumList);

            map.put("code",Code.SUCCESS);
            map.put("msg","操作成功");
            map.put("data",data);
        }else{
            map.put("code",Code.OUT_OF_LENGTH);
            map.put("msg","排名长度超出");
        }
        return map;
    }

    /**
     * @deprecation 获取村庄的集体经济排行
     * @param rank Integer 排行数
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-08 8:58
     */
    @ResponseBody
    @RequestMapping("/selectVillageRank")
    public Map<String,Object> selectVillageRank(@Param("rank") Integer rank){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();
        if(rank == null){
            map.put("code",Code.PARAM_LESS);
            map.put("msg","缺少请求参数");
        }else{

            List<FpVillage> villageList = selectS.findVillageOrderByAllIncome(rank);
            data.put("villageList",villageList);

            map.put("code",Code.SUCCESS);
            map.put("msg","操作成功");
            map.put("data",data);
        }
        return map;
    }


    /**
     * @deprecation 获取乡镇的居民满意度
     * @param townId Integer 乡镇id
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-09 10:39
     */
    @ResponseBody
    @RequestMapping("/findSatisfyByTownId")
    public Map<String,Object> findSatisfyByTownId(@Param("townId") Integer townId){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        if(townId == null){
            map.put("code",Code.PARAM_LESS);
            map.put("msg","缺少请求参数");
        }else if(selectS.checkHasTown(townId)){//检测乡镇是否存在

            FpSatisfy satisfy = selectS.findSatisfyByTownId(townId);
            data.put("satisfy",satisfy);

            map.put("code",Code.SUCCESS);
            map.put("msg","操作成功");
            map.put("data",data);
        }else{
            map.put("code",Code.TOWN_NOT_FIND);
            map.put("msg","该乡镇不存在");
        }

        return map;
    }


    /**
     * @deprecation 获取乡镇的居民评价表
     * @param townId Integer 乡镇id
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-09 10:39
     */
    @ResponseBody
    @RequestMapping("/findEvaluationByTownId")
    public Map<String,Object> findEvaluationByTownId(@Param("townId") Integer townId){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        if(townId == null){
            map.put("code",Code.PARAM_LESS);
            map.put("msg","缺少请求参数");
        }else if(selectS.checkHasTown(townId)){//检测乡镇是否存在

            FpEvaluation evaluation = selectS.findEvaluationByTownId(townId);
            data.put("evaluation",evaluation);

            map.put("code",Code.SUCCESS);
            map.put("msg","操作成功");
            map.put("data",data);
        }else{
            map.put("code",Code.TOWN_NOT_FIND);
            map.put("msg","该乡镇不存在");
        }

        return map;
    }


    /**
     * @deprecation 获取该平台的总统计数据
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-09 15:09
     */
    @ResponseBody
    @RequestMapping("/findSum")
    public Map<String,Object> findSum(){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        FpAllSum sum = selectS.findSumLimitOne();
        data.put("sum",sum);

        map.put("code",Code.SUCCESS);
        map.put("msg","操作成功");
        map.put("data",data);

        return map;
    }

    /**
     * @deprecation 获取一个乡镇的详细信息
     * @param townId Integer 乡镇id
     * @return FpTown
     * @author 雷楚桥
     * @date 2018-09-10 11:05
     */
    @ResponseBody
    @RequestMapping("/getTown")
    public Map<String,Object> getTown(@Param("townId") Integer townId){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        if(townId == null){
            map.put("code",Code.PARAM_LESS);
            map.put("msg","缺少请求参数");
        }else if(selectS.checkHasTown(townId)){//检测乡镇是否存在

            FpTown town = selectS.findOneTown(townId);
            data.put("town",town);

            map.put("code",Code.SUCCESS);
            map.put("msg","操作成功");
            map.put("data",data);
        }else{
            map.put("code",Code.TOWN_NOT_FIND);
            map.put("msg","该乡镇不存在");
        }
        return map;
    }


    /**
     * @deprecation 检测乡镇是否存在
     * @param townId Integer 乡镇id
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-10 15:08
     */
    @ResponseBody
    @RequestMapping("/checkHasTown")
    public Map<String,Object> checkHasTown(@Param("townId") Integer townId){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        if(townId == null){
            map.put("code",Code.PARAM_LESS);
            map.put("msg","缺少请求参数");
        }else{
            data.put("has",selectS.checkHasTown(townId));
            map.put("data",data);

            map.put("code",Code.SUCCESS);
            map.put("msg","请求成功");
        }
        return map;
    }


    /**
     * @deprecation 检测村庄是否存在
     * @param villageId Integer 村庄id
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-10 15:13
     */
    @ResponseBody
    @RequestMapping("/checkHasVillage")
    public Map<String,Object> checkHasVillage(@Param("villageId") Integer villageId){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        if(villageId == null){
            map.put("code",Code.PARAM_LESS);
            map.put("msg","缺少请求参数");
        }else{
            data.put("has",selectS.checkHasVillage(villageId));
            map.put("data",data);

            map.put("code",Code.SUCCESS);
            map.put("msg","请求成功");
        }
        return map;
    }

    /**
     * @deprecation 获取一个乡镇下的所有家庭
     * @param townId Integer 乡镇id
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-10 15:17
     */
    @ResponseBody
    @RequestMapping("/findHouseholdByTownId")
    public Map<String,Object> findHouseholdByTownId(@Param("townId") Integer townId){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        if(townId == null){
            map.put("code",Code.PARAM_LESS);
            map.put("msg","缺少请求参数");
        }else if(selectS.checkHasTown(townId)){//检测乡镇是否存在

            List<FpHousehold> householdList = selectS.findHouseholdByTownId(townId);
            data.put("householdList",householdList);

            map.put("code",Code.SUCCESS);
            map.put("msg","操作成功");
            map.put("data",data);
        }else{
            map.put("code",Code.TOWN_NOT_FIND);
            map.put("msg","该乡镇不存在");
        }
        return map;
    }

    /**
     * @deprecation 统计家庭数量
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-10 15:13
     */
    @ResponseBody
    @RequestMapping("/countAllHouseHold")
    public Map<String,Object> countAllHouseHold(){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        data.put("count",selectS.countAllHouseHold());
        map.put("data",data);

        map.put("code",Code.SUCCESS);
        map.put("msg","请求成功");
        return map;
    }


    /**
     * @deprecation 分页获取指定页数的家庭
     * @param page Integer 页数
     * @param offset Integer 每页的数量
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-10 15:24
     */
    @ResponseBody
    @RequestMapping("/findHouseHoldLimit")
    public Map<String,Object> findHouseHoldLimit(@Param("page") Integer page,
                                                 @Param("offset") Integer offset) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> data = new HashMap<>();

        if (page == null || offset == null) {
            map.put("code", Code.PARAM_LESS);
            map.put("msg", "缺少请求参数");
        } else {
            List<FpHousehold> householdList = selectS.findHouseHoldLimit(page * offset, offset);
            data.put("householdList", householdList);

            map.put("code", Code.SUCCESS);
            map.put("msg", "操作成功");
            map.put("data", data);
        }
        return map;
    }


    /**
     * @deprecation 获取一个家庭的人员数量
     * @param householdId Integer 家庭id
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-10 15:40
     */
    @ResponseBody
    @RequestMapping("/countResidentByHouseholdId")
    public Map<String,Object> countResidentByHouseholdId(@Param("householdId") Integer householdId){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        data.put("count",selectS.countResidentByHouseholdId(householdId));
        map.put("data",data);

        map.put("code",Code.SUCCESS);
        map.put("msg","请求成功");
        return map;
    }

    /**
     * @deprecation 获取一个乡镇所有家庭收入之和
     * @param townId Integer乡镇id
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-10 15:46
     */
    @ResponseBody
    @RequestMapping("/sumHouseholdAllIncomeByTownId")
    public Map<String,Object> sumHouseholdAllIncomeByTownId(@Param("townId") Integer townId){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        data.put("count",selectS.sumHouseholdAllIncomeByTownId(townId));
        map.put("data",data);

        map.put("code",Code.SUCCESS);
        map.put("msg","请求成功");
        return map;
    }


    /**
     * @deprecation 获取一个乡镇的所有家庭的数量
     * @param townId Integer 乡镇id
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-10 16:06
     */
    @ResponseBody
    @RequestMapping("/countHouseholdByTownId")
    public Map<String,Object> countHouseholdByTownId(@Param("townId") Integer townId){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        data.put("count",selectS.countHouseholdByTownId(townId));
        map.put("data",data);

        map.put("code",Code.SUCCESS);
        map.put("msg","请求成功");
        return map;
    }


    /**
     * @deprecation 统计一个乡镇的人口数量
     * @param townId Integer 乡镇id
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-10 16:08
     */
    @ResponseBody
    @RequestMapping("/countResidentByTownId")
    public Map<String,Object> countResidentByTownId(@Param("townId") Integer townId){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        data.put("count",selectS.countResidentByTownId(townId));
        map.put("data",data);

        map.put("code",Code.SUCCESS);
        map.put("msg","请求成功");
        return map;
    }


    /**
     * @deprecation 检测乡镇排名长度是否超出乡镇的总数
     * @param rank Integer 排名
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-10 16:10
     */
    @ResponseBody
    @RequestMapping("/checkHasTownRank")
    public Map<String,Object> checkHasTownRank(@Param("rank") Integer rank){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        data.put("has",selectS.checkHasTownRank(rank));
        map.put("data",data);

        map.put("code",Code.SUCCESS);
        map.put("msg","请求成功");
        return map;
    }


    /**
     * @deprecation 查询所有乡镇统计
     * @return Map
     * @author 雷楚桥
     * @date 2018-09-11 10:44
     */
    @ResponseBody
    @RequestMapping("/findAllSum")
    public Map<String,Object> findAllSum(){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> data = new HashMap<>();

        data.put("sumList",selectS.findAllTownSum());

        map.put("data",data);

        map.put("code",Code.SUCCESS);
        map.put("msg","请求成功");
        return map;
    }

}
